import { createRouter, createWebHistory } from 'vue-router'
import Type from "@/views/Type";
import Tag from "@/views/Tag";
import Archive from "@/views/Archive";
import About from "@/views/About";
import UserBase from "@/views/UserBase";
import AdminBase from "@/views/admin/AdminBase";
import AdminType from "@/views/admin/AdminType";
import AdminTag from "@/views/admin/AdminTag";
import AdminBlog from "@/views/admin/AdminBlog";
import TypeInput from "@/views/admin/TypeInput";
import TagInput from "@/views/admin/TagInput";
import BlogInput from "@/views/admin/BlogInput";
import Index from "@/views/Index";
import Blog from "@/views/Blog";
import Login from "@/views/admin/Login";

const routes = [
  {
    path: '/',
    component: UserBase,
    meta: {
      keepAlive: true
    },
    children: [
      {
        path: 'type',
        component: Type
      },
      {
        path: 'tag',
        component: Tag
      },
      {
        path: 'archive',
        component: Archive
      },
      {
        path: 'about',
        component: About
      },
      {
        path: '/',
        component: Index
      },
      {
        path: 'blog',
        component: Blog
      }
    ]
  },
  {
    path: '/admin',
    component: AdminBase,
    children: [
      {
        path: 'type',
        component: AdminType
        // meta:{
        //   keepalive: true
        // }
      },
      {
        path: 'tag',
        component: AdminTag
      },
      {
        path: 'blog',
        component: AdminBlog,
        meta: {
          keepAlive: true
        }
      },
      {
        path: 'type-input',
        component: TypeInput
      },
      {
        path: 'tag-input',
        component: TagInput
      },
      {
        path: 'blog-input',
        component: BlogInput
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next) =>{
  const username = sessionStorage.getItem('username');
  // console.log('token:',token)
  // console.log(to.name);
  // console.log(to.path.substring(0,6))
  // alert(user);
  let str = to.path.substring(0,6);
  // alert(username);
  if (str === '/admin' && username === null){
    next({
      name: 'login'
    })
  }else next();
  // if(to.path !=='login'){
  //   // alert(212);
  //   next({
  //     name:'login'
  //   })  //没去登录页，还没token，跳转去登录页
  // }
  // else next()  //正常跳转
  // next();
})


export default router
